package com.bungarus.group.model;

import com.bungarus.group.cachestore.CacheStore;
import com.bungarus.group.cachestore.CacheTransformer;
import com.bungarus.group.cachestore.StoreColumn;
import com.bungarus.group.cachestore.StoreRow;
import org.springframework.stereotype.Component;

import java.util.LinkedHashSet;
import java.util.Set;

@Component
public class MemberCacheTransformer implements CacheTransformer<Member> {
    private static final String NAME = "member";
    private static final String MEMBER_NAME = "_n_";
    private static final String MEMBER_ACCOUNT = "_a_";
    private static final String MEMBER_TENANT_ID = "_tid_";
    private static final String MEMBER_ID = "_mid_";
    private static final String MEMBER_PASSWORD = "_p_";

    @Override
    public String getCacheTableName() {
        return NAME;
    }

    @Override
    public StoreRow transform(Member source) {
        StoreRow.Builder builder = StoreRow.newBuilder();
        Set<StoreColumn> columns = new LinkedHashSet<>();

        StoreColumn column = new StoreColumn();
        column.setColumnName(MEMBER_NAME);
        column.setColumnValue(source.getName());
        columns.add(column);

        column = new StoreColumn();
        column.setColumnName(MEMBER_ACCOUNT);
        column.setColumnValue(source.getAccountName());
        columns.add(column);

        column = new StoreColumn();
        column.setColumnName(MEMBER_PASSWORD);
        column.setColumnValue(source.getPassword());
        columns.add(column);

        column = new StoreColumn();
        column.setColumnName(MEMBER_TENANT_ID);
        column.setColumnValue(String.valueOf(source.getTenantId()));
        columns.add(column);

        column = new StoreColumn();
        column.setColumnName(MEMBER_ID);
        column.setColumnValue(String.valueOf(source.getId()));
        columns.add(column);

        return builder.table(NAME)
                .id(CacheStore.ofUniqueId(String.valueOf(source.getTenantId()), source.getAccountName()))
                .values(columns)
                .build();
    }

    @Override
    public Member reverse(StoreRow row) {
        Set<StoreColumn> columns = row.getRowValues();
        Member.Builder memBuilder = Member.newBuilder();
        columns.forEach(column -> {
            if(column.getColumnName().equals(MEMBER_NAME)) {
                memBuilder.name(column.getColumnValue());
            }
            else if(column.getColumnName().equals(MEMBER_ACCOUNT)) {
                memBuilder.account(column.getColumnValue());
            }
            else if(column.getColumnName().equals(MEMBER_ID)) {
                memBuilder.id(Long.valueOf(column.getColumnValue()));
            }
            else if(column.getColumnName().equals(MEMBER_TENANT_ID)) {
                memBuilder.tenantId(Long.valueOf(column.getColumnValue()));
            }
            else if(column.getColumnName().equals(MEMBER_PASSWORD)) {
                memBuilder.pwd(column.getColumnValue());
            }
            else
                throw new IllegalStateException();
        });
        return memBuilder.build();
    }
}
